/**
 * Created by 小敏哥 on 2017/6/2.
 */

function Pay(channel){
    var url=window.location.href.indexOf('192.168.1.165')>=0?'http://192.168.1.165:9071/':'https://webservice.cx580.com/';
    var activity='LIGHT_COUPONS_201706';
    this.pay=function(){
        if(channel=='aliPay'||channel=='weixin'||channel=='qq') {
            checkOutCounterPay(function(result){
                location.href=result;
            });
        }
        else if(channel=='app'){
            appPay(function(orderId){
                payNotification(orderId,function(notifyResult){
                    if(notifyResult.data.result=='1'){
                        window.location.replace('./afterpay.html?app=app&_wv=1027');
                    }
                    else {
                        if(notifyResult.msg) {
                            AW.toast.show({
                                message: notifyResult.msg,
                                type: 'none',
                                callContainer: true
                            });
                        }
                        //alert(resultData.msg);
                    }
                })
            })
        }
    };


    //支付通知
    function payNotification(orderId,callBack){
        $.ajax({
            url: url+'order/payNotify',
            type: "POST",
            data: {
                userid:sessionStorage.getItem('userId'),
                token:sessionStorage.getItem('token'),
                usertype:sessionStorage.getItem('userType'),
                orderId:orderId
            },
            success:function(resultData){
                resultData=JSON.parse(resultData);
                if (resultData.code == '1000') {
                    callBack && callBack(resultData);
                }
                else {
                    AW.toast.show({
                        message: resultData.msg,
                        type: 'none',
                        callContainer: true
                    });
                }
            },
            error:function(e){
               /* alert(JSON.stringify(e));*/
            }

        });
    }

    //收银台
    function checkOutCounterPay(callBack){
        $.ajax({
            url: url+'order/pay',
            type: "POST",
            data: {
                userid:sessionStorage.getItem('userId'),
                token:sessionStorage.getItem('token'),
                usertype:sessionStorage.getItem('userType'),
                activity:activity
            },
            success:function(data){
                if(callBack){
                    data=JSON.parse(data);
                    if(data.code=='1000') {
                        callBack(data.data.paySign);
                    }
                    else{
                        AW.toast.show({
                            message: data.msg,
                            type: 'none',
                            callContainer: true
                        });
                    }
                }
            },
            error:function(e){
                /*alert(e);*/
            }

        });
    }


    //app支付
    function appPay(paySuccessCallBack){
        $.ajax({
            url: url+'order/payApp',
            type: "POST",
            data: {
                userid:sessionStorage.getItem('userId'),
                token:sessionStorage.getItem('token'),
                usertype:sessionStorage.getItem('userType'),
                payType:'3',
                activity:activity
            },
            success:function(resultData){
                resultData=JSON.parse(resultData);
                if(resultData.code=='1000') {
                    if (paySuccessCallBack) {
                        var content = {
                            "commandId": "",
                            "command": "payOrder",
                            "data": {
                                "orderId": resultData.data.orderId,
                                "orderType": resultData.data.orderType,
                                "orderAmt": resultData.data.payAmount,
                                "payType": resultData.data.payAmount > 0 ? resultData.data.payType : '17',
                                "token": resultData.data.paySignToken
                            }
                        };
                        window.cx580.jsApi.call(content, function (result) {
                            if (result.data.tradeStatus == '1') {
                                paySuccessCallBack && paySuccessCallBack(resultData.data.orderId)
                            }
                            else {
                                var message = '';
                                switch (result.data.tradeStatus) {
                                    case '2':
                                        message = '支付失败';
                                        break;
                                    case '3':
                                        message = '网络超时';
                                        break;
                                    case '4':
                                        message = '用户信息登录超时';
                                        break;
                                    case '5':
                                        message = '支付软件没安装或者不支持该支付';
                                        break;
                                }
                                AW.toast.show({
                                    message: message,
                                    type: 'none',
                                    callContainer: true
                                });
                            }
                        });
                    }
                }
                else{
                    AW.toast.show({
                        message: resultData.msg,
                        type: 'none',
                        callContainer: true
                    });
                }
            },
            error:function(e){
                /*alert(e);*/
            }

        });
    }
}